import { http } from "../index";
import {managerMapper} from "../../service/mapper/manager/ManagerMapper";
import Manager from "../../service/entity/Manager";
import RedisClient from "../../service/redis/RedisClient";
import Constant from "../../service/Constant";
import Utils from "../../service/utils/Utils";

let redisClient = new RedisClient();

http.router.register('manager', {
    '/manager/list': {
        options: {
            method: 'GET',
            name: '管理员列表',
        },
        async handler(ctx) {
            let managers:any = await managerMapper.findAll();
            if(managers != null && managers.length > 0){
                let managerIds: string[] = [];
                managers.forEach(function (item: { managerId: string; }, index: any) {
                    managerIds.push(item.managerId)
                })
                let stars = await redisClient.mget(managerIds);
                managers.forEach(function (item: { likeCount: any; managerId: string | number; }, index: number) {
                    item.likeCount = stars[index] == null?0:<number>stars[index];
                })
                managers = managers.sort(function (x: { likeCount: number; }, y: { likeCount: number; }) {
                    return y.likeCount - x.likeCount
                })
            }
            return managers
        },
    },
    '/manager/findByMobile': {
        options: {
            method: 'GET',
            name: '管理员列表',
        },
        async handler(ctx) {
            let manager = new Manager();
            manager.mobile = ctx.request.query.mobile;
            let managers = await managerMapper.findByVo(manager);
            let result:any;
            if(!managers || managers.length == 0){
                result = Constant.ERROR_MOBILE_NOT_FOUNT;
            }else{
                result = managers[0];
                result.likeCount = await redisClient.getStar(result.managerId)
            }
            return result;
        },
    },
    '/manager/starByMobile': {
        options: {
            method: 'GET',
            name: '管理员列表',
        },
        async handler(ctx) {
            let manager = new Manager();
            manager.mobile = ctx.request.query.mobile;
            let managers = await managerMapper.findByVo(manager);
            let result:any;
            if(!managers || managers.length == 0){
                result = Constant.ERROR_MOBILE_NOT_FOUNT;
            }else{
                result = managers[0];
                result.likeCount = await redisClient.incr(result.managerId)
            }
            return result;
        },
    },
    '/manager/updateByMobile': {
        options: {
            method: 'POST',
            name: '管理员列表',
        },
        async handler(ctx) {
            let manager:any = <Manager>ctx.request.body;
            manager.managerId = null;
            let updateInfo = <Manager>{
                name:manager.name,
                mail:manager.mail,
                workId:manager.workId,
                status:manager.status,
                mobile:manager.mobile,
            }
            let num = await managerMapper.updateByMobile(updateInfo);
            if(num != 1){
                manager = {};
            }
            return manager;
        },
    },
    '/manager/add': {
        options: {
            method: 'POST',
            name: '管理员列表',
        },
        async handler(ctx) {
            let result: any = {};
            let managerInfo = <Manager>ctx.request.body;
            //参数校验
            if(Utils.isNotEmpty(managerInfo.mobile) && Utils.isNotEmpty(managerInfo.managerId)){
                let isExist = await managerMapper.isExistByManagerIdOrMobile(<string>managerInfo.managerId,<string>managerInfo.mobile);
                if (!isExist) {
                    let managerId = await managerMapper.insert(managerInfo)
                    if(managerId){
                        result = managerInfo;
                    }
                }else{
                    result = Constant.ERROR_MOBILE_EXIST;
                }
            }else{
                result = Constant.ERROR_PARAM;
            }
            return result;
        },
    },
})
